﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Utility.Web;

namespace XDeal.Common.Web
{
    /// <summary>
    /// Summary description for PagerLink
    /// Format : Fisrt Prev 1 2 3 4 5 6 7 8 9 10 Next Last
    /// </summary>
    public class Pager
    {
        static string lblPage = "Trang: ";
        public static string GenerateFrontEndLink(int toltalItems, int pageSize, int itemsPerPage, int currPage)
        {
            string pageLink = "", pagingPreview = "", pagingNum = "", pagingNext = "";
            int totalPage = (toltalItems % itemsPerPage == 0) ? (toltalItems / itemsPerPage) : (toltalItems / itemsPerPage + 1);
            if (currPage > totalPage)
                currPage = totalPage;
            if (toltalItems > itemsPerPage)//Upto 2 pages
            {
                /*---Show Fisrt Prev Page--*/
                if (currPage > 1)
                {
                    pagingPreview = "<a href=\"#1\" paging=\"" + (currPage - 1) + "\" class=\"btnFirst\" title = \"Trang đầu\"></a>";
                }

                if (currPage < totalPage)
                {
                    pagingNext = "<a href=\"#" + totalPage + "\" paging=\"" + (currPage - 1) + "\" class=\"btnLast\"  title = \"Trang cuối\"></a>";
                }

                int start = currPage - (int)(pageSize / 2);
                int end = currPage + (int)((pageSize / 2) - 1);
                if (start < 1)
                {
                    start = 1; end = (pageSize > totalPage) ? totalPage : pageSize;
                }
                if (end > totalPage)
                {
                    end = totalPage; start = ((totalPage - pageSize + 1) > 1) ? (totalPage - pageSize + 1) : 1;
                }
                pagingNum += "<span class=\"spText\">" + lblPage + "</span>";
                pagingNum += pagingPreview;
                for (int i = start; i <= end; i++)
                {
                    if (i == currPage)
                        pagingNum += "<span class=\"selected\">" + i + "</span>";
                    else
                    {
                        pagingNum += "<a href=\"#" + i + "\" paging=\"" + i + "\" class=\"btnNomarl\">" + i + "</a>";
                    }
                }
            }
            pageLink += pagingNum + pagingNext;
            return pageLink;
        }

        public static string GenerateLink(int toltalItems, int pageSize, int itemsPerPage, int currPage, string pageURL)
        {
            if (pageURL.LastIndexOf('?') == -1)
                pageURL = pageURL + "?page=";
            else
            {
                int index = pageURL.LastIndexOf("page=");
                if (index == -1)
                    pageURL = pageURL + "&page=";
                else
                {
                    string newUrl = pageURL.Substring(index, pageURL.Length - index);
                    string[] lst = newUrl.Split('&');
                    pageURL = pageURL.Replace(lst[0], "") + "&page=";
                    pageURL = pageURL.Replace("&&", "&").Replace("?&", "?");
                }
            }
            return GenerateFrontEndLink(toltalItems, pageSize, itemsPerPage, currPage).Replace("#", pageURL);
        }
    }
}